<?php

/**
 * 前台控制器基类
 */
class FirstendAction extends TopAction
{
    protected $visitor = null;
    public $APPID = 'wx150ac18e5794e376';
    private $APPSECRET = 'a6313b68945cdc317169caa119a81e0b';
    //access_token,2小时有效期
    public $accessToken = null;
        //ticket,JS调用认证,2小时有效期
    public $ticket = null;
        //随机字符串
    public static $noncestr = null;
        //时间戳
    public static $timestamp = null;
        //签名
    public $signature = null;
        //保存这个长str
    public $ticketStr = null;
    public $url = null;
    public function _initialize()
    {
        parent::_initialize();
        // 网站状态
        if (!C('yh_site_status')) {
            header('Content-Type:text/html; charset=utf-8');
            exit(C('yh_closed_reason'));
        }
        $this->_init_visitor();
        $this->_assign_oauth();
        $this->_cate_mod = D('items_cate')->cache(true, 30 * 60);
        $this->cur_url = strtolower($_SERVER["REQUEST_URI"]);
        $this->assign('nav_curr', $this->cur_url);
        $catetree = $this->_cate_mod->where('status=1')->field('id,name')->order('id asc')->select();
        $this->assign('catetree', $catetree);
        $this->assign('request_url', $this->cur_url);
        $this->openid = I('openid', '', 'trim');
        $this->backurl = I('backurl', '', 'trim');
        if (!empty($this->openid)) {
            $this->_wechat_login($this->openid, $this->backurl);

        }
//        $this->tqkuid = C('yh_app_kehuduan');
//        $tkapi = F('tqkapi/api');
//        if (false === $tkapi) {
//            $this->tqkapi = 'http://api.tuiquanke.com';
//        } else {
//            $this->tqkapi = $tkapi;
//        }
        self::$timestamp = time();
        self::$noncestr = $this->getNoncestr();
    }


    protected function parent_pid()
    {
        $pid = trim(C('yh_taobao_pid'));
        $apppid = explode('_', $pid);
        return '_' . $apppid[3];
    }


    protected function agent_pid()
    {
        $track_val = cookie('trackid');
        if (!empty($track_val)) {
            $track = '_' . base64_decode($track_val);
            $par_pid = $this->parent_pid();
            $pid = str_replace($par_pid, $track, trim(C('yh_taobao_pid')));
            return $pid;
        } else {
            return '';
        }

    }

    public function getIP()
    {
        $IP = $_SERVER['REMOTE_ADDR'];
        return $IP;
    }

    /**
     * 初始化访问者
     */
    private function _init_visitor()
    {
        $this->visitor = new user_visitor();
        $this->assign('visitor', $this->visitor->info);
    }


    protected function create_token($key, $data = array())
    {
        $ServerToken = '';
        foreach ($data as $k => $v) {
            $ServerToken .= md5($v);
        }
        $ServerToken = md5($ServerToken . '_' . $key);
        return $ServerToken;
    }

    /**
     * 第三方登陆模块
     */
    private function _assign_oauth()
    {
        if (false === $oauth_list = F('oauth_list')) {
            $oauth_list = D('oauth')->oauth_cache();
        }
        $this->assign('oauth_list', $oauth_list);
    }

    protected function _wechat_login($openid, $backurl)
    {

        switch ($backurl) {
            case "zhibo":
                $backurl = U('zhibo/index');
                break;
            case "record":
                $backurl = U('user/record');
                break;
            case "order":
                $backurl = U('user/order');
                break;
            case "suborder":
                $backurl = U('user/suborder');
                break;
            default:
                $backurl = U('user/ucenter');
                break;
        }

        $errurl = U('user/ucenter');
        if (!empty($openid) && strlen($openid) > 20) {

            $where = array(
                'openid' => $openid,
            );
            $U = M('user');

            $exit_openid = $U->where($where)->count();

            if ($exit_openid > 0) {

                $res = $this->visitor->wechatlogin($openid);

                if ($res) {

                    redirect($backurl);

                } else {

                    redirect($errurl);

                }

            } else {

                $data = array(
                    'username' => 'wx_' . substr($openid, 20, 6),
                    'nickname' => 'wx_' . substr($openid, 20, 6),
                    'password' => md5(substr($openid, 20, 6)),
                    'reg_ip' => get_client_ip(),
                    'avatar' => '/static/tqkpc/images/noimg.png',
                    'state' => 1,
                    'status' => 1,
                    'reg_time' => time(),
                    'last_time' => time(),
                    'create_time' => time(),
                    'openid' => $openid,
                );
                $res = $U->add($data);

                $res = $this->visitor->wechatlogin($openid);

                redirect($errurl);

            }


        }


    }

    /**
     * SEO设置
     */
    protected function _config_seo($seo_info = array(), $data = array())
    {
        $page_seo = array(
            'title' => C('yh_site_title'),
            'keywords' => C('yh_site_keyword'),
            'description' => C('yh_site_description')
        );
        $page_seo = array_merge($page_seo, $seo_info);
        // 开始替换
        $searchs = array(
            '{site_name}',
            '{site_title}',
            '{site_keywords}',
            '{site_description}'
        );
        $replaces = array(
            C('yh_site_name'),
            C('yh_site_title'),
            C('yh_site_keyword'),
            C('yh_site_description')
        );
        preg_match_all("/\{([a-z0-9_-]+?)\}/", implode(' ', array_values($page_seo)), $pageparams);
        if ($pageparams) {
            foreach ($pageparams[1] as $var) {
                $searchs[] = '{' . $var . '}';
                $replaces[] = $data[$var] ? strip_tags($data[$var]) : '';
            }
            // 符号
            $searchspace = array(
                '((\s*\-\s*)+)',
                '((\s*\,\s*)+)',
                '((\s*\|\s*)+)',
                '((\s*\t\s*)+)',
                '((\s*_\s*)+)'
            );
            $replacespace = array(
                '-',
                ',',
                '|',
                ' ',
                '_'
            );
            foreach ($page_seo as $key => $val) {
                $page_seo[$key] = trim(preg_replace($searchspace, $replacespace, str_replace($searchs, $replaces, $val)), ' ,-|_');
            }
        }
        $this->assign('page_seo', $page_seo);
    }

    /**
     * 连接用户中心
     */
    protected function _user_server()
    {
        $passport = new passport(C('yh_integrate_code'));
        return $passport;
    }

    /**
     * 前台分页统一
     */
    protected function _pager($count, $pagesize, $path = null)
    {
        $pager = new Page($count, $pagesize);
        if ($path) {
            $pager->path = $path;
        }
        $pager->rollPage = 3;
        $pager->setConfig('header', '条记录');
        $pager->setConfig('prev', '上一页');
        $pager->setConfig('next', '下一页');
        $pager->setConfig('first', '第一页');
        $pager->setConfig('last', '最后一页');
        $pager->setConfig('theme', '%upPage% %first% %linkPage% %end% %downPage%');
        return $pager;
    }

    protected function _curl($url, $data = array(), $is_post = false, $cookie = null, $referer = null)
    {
        set_time_limit(30);
        $curl = curl_init();
        if ($data && $is_post == false) {
            $url .= '?' . http_build_query($data);
        }
        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //
        curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if ($is_post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        }
        if ($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        curl_setopt($curl, CURLOPT_REFERER, C('yh_site_url'));
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        $result = curl_exec($curl);
        Log::write('执行请求：' . $url . '请求参数' . json_encode($data) . '返回结果---' . json_encode($result), Log::INFO, '', 'curl');
        //var_dump(curl_error($curl));
        curl_close($curl);
        if (empty($result)) {
            $result = false;
        }

        return $result;
    }

    protected function isMobile()
    {
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
            return true;
        }
        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset ($_SERVER['HTTP_VIA'])) {
            // 找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
        }
        // 脑残法，判断手机发送的客户端标志,兼容性有待提高
        if (isset ($_SERVER['HTTP_USER_AGENT'])) {
            $clientkeywords = array('nokia',
                'sony',
                'ericsson',
                'mot',
                'samsung',
                'htc',
                'sgh',
                'lg',
                'sharp',
                'sie-',
                'philips',
                'panasonic',
                'alcatel',
                'lenovo',
                'iphone',
                'ipod',
                'blackberry',
                'meizu',
                'android',
                'netfront',
                'symbian',
                'ucweb',
                'windowsce',
                'palm',
                'operamini',
                'operamobi',
                'openwave',
                'nexusone',
                'cldc',
                'midp',
                'wap',
                'mobile'
            );
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
                return true;
            }
        }
        // 协议法，因为有可能不准确，放到最后判断
        if (isset ($_SERVER['HTTP_ACCEPT'])) {
            // 如果只支持wml并且不支持html那一定是移动设备
            // 如果支持wml和html但是wml在html之前则是移动设备
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
                return true;
            }
        }
        return false;
    }


    public function _empty()
    {
        $this->display(ACTION_NAME);
    }

    //获取token
    public function getWxAccessToken()
    {
        // 获取缓存数据
        try{
            $Data = S('wxAccessToken1');
            if (!$Data || $Data == null) {
                $appid = $this->APPID;
                $appsecret = $this->APPSECRET;
                $url = 'https://api.weixin.qq.com/cgi-bin/token';
                $_data = [
                    'grant_type' => "client_credential",
                    'appid' => $appid,
                    'secret' => $appsecret
                ];
                $result = $this->_curl($url, $_data);
                if ($result && $result->errcode == 0) {
                    $data = json_decode($result, true);
                    $Data = $data['access_token'];
                    S('wxAccessToken1', $Data, ['expire'=>$data['expires_in']]);
                    $result = ['status' => 'ok', 'access_token' => $Data];
                } else {
                    throw new \Exception(   '请求微信出错啦');
                }
            } else {
                $result = ['status' => 'ok', 'access_token' => $Data];
            }
            $this->accessToken = $Data;
            Log::write('获取accesstoken返回结果---' . json_encode($result), Log::INFO, '', 'access_token');
            return $result;
        }catch (\Exception $e ){
            $msg = $e->getMessage();
            exit(json_encode(['status'=>'fail', 'msg'=>$msg]));
        }

    }

    //根据token获取jsapi_ticket
    public function getWxTicket($accesstoken = null)
    {
        try{
            if (!$accesstoken) {
                $_accesstoken = $this->getWxAccessToken();
                $accesstoken = $_accesstoken['access_token'];
            }
            // 获取缓存数据
            $Data = S('wxTicket1');
            if (!$Data || $Data == null) {
                $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
                $_data = [
                    'access_token' => $accesstoken,
                    'type' => 'jsapi'
                ];
                $result = $this->_curl($url, $_data);
                if ($result && $result->errcode == 0) {
                    $data = json_decode($result, true);
                    $Data = $data['ticket'];
                    S('wxTicket1',  $Data, ['expire'=>$data['expires_in']]);
                    $result = ['status' => 'ok', 'ticket' => $Data];
                } else {
                    throw new \Exception(   '请求微信出错啦','fail');
                }
            } else {
                $result = ['status' => 'ok', 'ticket' => $Data];
            }
            Log::write('获取ticket返回结果---' . json_encode($result), Log::INFO, '', 'ticket');
            $this->ticket = $Data;
            return $result;
        }catch (\Exception $e){
            $msg = $e->getMessage();
            exit(json_encode(['status'=>'fail', 'msg'=>$msg]));
        }

    }

    public function getWXsign($ticket)
    {

    }

    public function getTimeStamp()
    {
        return self::$timestamp;
    }

    public function getNoncestr($len = 16)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $charsLen = strlen($chars);
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            $str .= substr($chars, mt_rand(0, $charsLen), 1);
        }
        return $str;
    }

    /**
     * 获取验证码
     */
    public function verify_code() {
        $type = I('get.type');
        if (!$type){
            $type='verify';
        }
        import("ORG.Util.Image");
        Image::buildImageVerify(4,5,'png','50','25', $type);
    }

    /**验证验证码是否正确
     * @param $code
     * @param string $type
     * @return bool|string
     */
    public function checkcode($code, $type='verify'){
        if($_SESSION[$type] != md5(strtolower($code))){
            return '验证码错误';
        }else{
            return true;
        }
    }

    function check_phone($phone){
        $check = '/^(1[23456789]\d{9}$/';
        if (preg_match($check, $phone)) {
            return true;
        } else {
            return false;
        }
    }

}